package com.Szjcyh.Until;

import java.util.Map;

import org.apache.commons.httpclient.NameValuePair;

import com.alibaba.fastjson.JSONObject;


public class Api {
	
    public static ResultModel buildRequest(String url,String jsonStr) throws Exception {
    	return buildRequest(url,"", "",jsonStr);

    }
    public static ResultModel buildRequest(String url,String strParaFileName, String strFilePath,String jsonStr) throws Exception {

        HttpProtocolHandler httpProtocolHandler = HttpProtocolHandler.getInstance();

        //HttpRequest request = new HttpRequest(HttpResultType.BYTES);
        HttpRequest request = new HttpRequest(HttpResultType.STRING);
        //设置编码集
        request.setCharset("UTF-8");
        //request.setUrl("http://120.24.241.134:8081/cgi-bin/zl_cgi");
        request.setUrl(url);

        HttpResponse response = httpProtocolHandler.execute(request,strParaFileName,strFilePath,jsonStr);
        if (response == null) {
            return null;
        }
        
        String strResult = response.getStringResult();
        
        System.out.println("strResult="+strResult);

        ResultModel jsonResult = JSONObject.parseObject(strResult, ResultModel.class);
        return jsonResult;
    }
    
    public static NameValuePair[] generatNameValuePair(Map<String, String> properties) {
        NameValuePair[] nameValuePair = new NameValuePair[properties.size()];
        int i = 0;
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            nameValuePair[i++] = new NameValuePair(entry.getKey(), entry.getValue());
        }
        
        return nameValuePair;
    }
    
    
    public static String generatNameValuePair2(NameValuePair[] nameValuePair) {
    	JSONObject jsonParam = new JSONObject(); 
    	  
        for (NameValuePair np:nameValuePair) {
        	jsonParam.put(np.getName(), np.getValue());
        }
        return jsonParam.toString();
    }

}
